package capricornus.item
{
	import capricornus.components.sprite.AbstractComponent;
	import capricornus.components.tooltips.ToolTipVO;
	import capricornus.components.tooltips.ToolTips;
	import capricornus.components.utils.FilterUtils;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.utils.Dictionary;
	
	[Event(name="dragItemStart", type="capricornus.item.ItemEvent")]
	[Event(name="dragItemDiscarded", type="capricornus.item.ItemEvent")]
	[Event(name="dragItemOver", type="capricornus.item.ItemEvent")]
	[Event(name="dragItemOverAndMoving", type="capricornus.item.ItemEvent")]
	[Event(name="dragItemOut", type="capricornus.item.ItemEvent")]
	[Event(name="dragItemDropped", type="capricornus.item.ItemEvent")]
	[Event(name="dragItemToOtherContainer", type="capricornus.item.ItemEvent")]
	[Event(name="doubleClick", type="capricornus.item.ItemEvent")]
	[Event(name="itemContainerLockOperate", type="capricornus.item.ItemContainerEvent")]
	public class ItemContainer extends AbstractComponent
	{
		private var _item:Item;
		
		private var _type:int;
		private var _dragMode:uint;
		
		private var _isLock:Boolean = false;
		
		private var _data:ToolTipVO;
		
		/**
		 * 
		 * @param type        容器的类型，不同的类型指示不同的地方，比如在背包里面或者邮件里面
		 * @param index       索引位置
		 * @param dragMode    拖动模式
		 * 
		 */		
		public function ItemContainer( type:int=ItemContainerType.KEEP, index:int=0, dragMode:uint=ItemDragMode.KEEP )
		{
			super();
			_type = type;
			_index = index;
			_dragMode = dragMode;
			init();
		}
		
		private function init():void
		{
			this.mouseChildren = false;
			this.doubleClickEnabled = true;
			
			_data = new ToolTipVO();
			
			if( _dragMode != ItemDragMode.KEEP )
			{
				ItemDragManager.instance.addContainer( this );
			}
			
			ToolTips.addTips( this, _data );
		}
		
		public function destroy():void
		{
			if( _dragMode != ItemDragMode.KEEP )
			{
				ItemDragManager.instance.removeContainer( this );
			}
		}
		
		public function get type():int
		{
			return _type;
		}
		
		public function get dragMode():uint
		{
			return _dragMode;
		}
		
		public function get isLock():Boolean
		{
			return _isLock;
		}
		
		public function lock():void
		{
			_enabled = false;
			_container.filters = FilterUtils.getGrayFilter();
			
			_isLock = true;
			_container.mouseChildren = false;
			_container.mouseEnabled = false;
			
			dispatchEvent( new ItemContainerEvent( ItemContainerEvent.ITEM_CONTAINER_LOCK_OPERATE, _isLock ) );
		}
		
		public function unlock():void
		{
			_enabled = true;
			_container.filters = null;
			
			_isLock = false;
			_container.mouseChildren = true;
			_container.mouseEnabled = true;
			
			dispatchEvent( new ItemContainerEvent( ItemContainerEvent.ITEM_CONTAINER_LOCK_OPERATE, _isLock ) );
		}
		
		/**
		 * 功能跟lock相同 
		 * @param value true：unlock(); false: lock();
		 * 
		 */		
		override public function set enabled(value:Boolean):void
		{
			if( value )
			{
				unlock();
			}
			else
			{
				lock();
			}
		}
		
		override public function get enabled():Boolean
		{
			return _isLock;
		}
		
		override public function setSize(w:int, h:int):void
		{
			super.setSize( w, h );
		}
		
		public function set item( value:Item ):void
		{
			if( _item==value ) return;
			
			if( _item != null )
			{
				_item.setParentContainer( null );
			}
			_data.data = null;
			while( _container.numChildren > 0 ) _container.removeChildAt(0);
			_item = value;
			if( _item != null )
			{
				_item.setParentContainer( this );
				
				_data.data = _item.attribute;
				
				_container.addChild( _item );
				_item.setSize( _width, _height ); 
			}
		}
		
		public function get item():Item
		{
			return _item;
		}
		
		/**
		 * 交换item 
		 * @param container
		 * 
		 */		
		public function exchangeItem( container:ItemContainer ):void
		{
			var it:Item = container.item;
			container.item = _item;
			this.item = it;
		}
		
		/**
		 * 转移item给container; container如果有item，则被销毁 
		 * @param container
		 * 
		 */		
		public function transferItemTo( container:ItemContainer ):void
		{
			var it:Item = container.item;
			if( it != null )
			{
				it.destroy();
			}
			
			it = _item;
			item = null;
			
			container.item = it;
		}
		
		/**
		 * 转移item的shortcut给container; container如果有item，则被销毁 
		 * @param container
		 * 
		 */		
		public function transferItemShortcutTo( container:ItemContainer ):void
		{
			if( _item==null ) return;
			
			var it:Item = container.item;
			if( it != null )
			{
				it.destroy();
			}
			
			container.item = getItemShortcut();
		}
		
		/**
		 * 销毁当前item 
		 * 
		 */		
		public function destroyCurrentItem():void
		{
			if( _item != null )
			{
				var it:Item = _item;
				item = null;
				it.destroy();
			}
		}
		
		public function getItemShortcut():ItemShortcut
		{
			if( _item != null )
			{
				var shortcut:ItemShortcut = new ItemShortcut();
				var it:Item = _item;
				if( it is ItemShortcut )
				{
					it = (it as ItemShortcut).parentItem;
				}
				shortcut.setParentItem( it );
				return shortcut;
			}
			return null;
		}
		
		internal function getShortcutImage():BitmapData
		{
			if( !isLock && _item != null )
			{
				var bd:BitmapData = new BitmapData( _container.height, _container.height, true, 0 );
				bd.draw( _container );
				
				return bd;
			}
			
			return null;
		}
	}
}